﻿using Prism.Commands;
using Quartz;
using Quartz.Impl.Matchers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace ZYing.QPanel.ViewModels
{
    public class JobGroupViewModel : ViewModelBase
    {
        public JobGroupViewModel(IScheduler scheduler, string group)
        {
            this.Scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
            this.Group = group ?? throw new ArgumentNullException(nameof(group));
            this.PauseCommand = new DelegateCommand(this.Pause, () => this.IsPaused == false).ObservesProperty(() => IsPaused);
            this.ResumeCommand = new DelegateCommand(this.Resume, () => this.IsPaused == true).ObservesProperty(() => IsPaused);
        }
        public IScheduler Scheduler { get; }
        public string Group { get; }
        private bool? _isPaused;
        public bool? IsPaused
        {
            get { return _isPaused; }
            set { SetProperty(ref _isPaused, value); }
        }
        public ICommand PauseCommand { get; }
        public ICommand ResumeCommand { get; }
        public async void Pause()
        {
            await Scheduler.PauseJobs(GroupMatcher<JobKey>.GroupEquals(this.Group));
            this.IsPaused = true;
        }
        public async void Resume()
        {
            await Scheduler.ResumeJobs(GroupMatcher<JobKey>.GroupEquals(this.Group));
            this.IsPaused = false;
        }
    }
}
